package com.zry.fertilizer.presenter

import android.content.Intent
import android.view.View
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView
import com.zry.fertilizer.R
import com.zry.fertilizer.R.id.searchOutTraceOrderET
import com.zry.fertilizer.adapter.OutTreasuryRecordAdapter
import com.zry.fertilizer.animation.RecyclerViewUtilKt
import com.zry.fertilizer.been.CustomerInfo
import com.zry.fertilizer.been.OutStock
import com.zry.fertilizer.been.PutState
import com.zry.fertilizer.retrofit.HttpServer
import com.zry.fertilizer.retrofit.ShowUserView
import com.zry.fertilizer.ui.activity.OutTreasuryInfoActivity
import com.zry.fertilizer.ui.activity.OutTreasuryRecordActivity
import com.zry.fertilizer.utils.ACacheUtil
import com.zry.fertilizer.utils.OneYardUtils
import com.zry.fertilizer.view.CustomerInfoPopup
import com.zry.fertilizer.view.PutStatePopup
import kotlinx.android.synthetic.main.activity_traceability.*

class OutTreasuryRecordPresenter(private val activity: OutTreasuryRecordActivity) : ShowUserView {
     var pageStoreIndex = 1
    private var outTreasuryRecordAdapter: OutTreasuryRecordAdapter? = null
    private var outStockBeanList: MutableList<OutStock>? = null


    fun outTreasuryRecord(view: View) {
        when (view.id) {
            R.id.searchOutTraceOrderBtn -> {
                pageStoreIndex = 1
                activity.customerID = ""
                activity.stateID = ""
                activity.outCode = activity.searchOutTraceOrderET.text.toString().trim()
                getOutTreasury(1, 999, activity.customerID, activity.stateID, activity.outCode)

            }
            R.id.outTraceStateLinear -> {
                PutStatePopup(activity, activity.outTraceStateLinear, OneYardUtils().getOutState()).setListener(object : PutStatePopup.PutStateListener {
                    override fun handleResult(putState: PutState) {
                        activity.outTraceStateTV.text = putState.putStorageName
                        activity.distributorTV.text = "经销商"
                        pageStoreIndex = 1
                        activity.customerID = ""
                        activity.stateID = putState.putStorageId.toString()
                        activity.outCode = ""
                        getOutTreasury(1, 999, activity.customerID, activity.stateID, activity.outCode)
                    }
                })
            }
            R.id.distributorLinear -> {
                HttpServer(this).getCustomerList(ACacheUtil().getLoginInfo().EntID, "", 1, 999, activity)
            }
        }
    }

    fun getOutTreasury(pageIndex: Int, pageSize: Int, CustomerID: String, StateID: String, OutCode: String) {
        HttpServer(this).getOutStockList(ACacheUtil().getLoginInfo().EntID, pageIndex, pageSize, CustomerID, StateID, OutCode, activity)
    }


    fun initRecycler() {
        outStockBeanList = mutableListOf()
        val recyclerViewUtilKt = RecyclerViewUtilKt(activity, activity.distributorRecycler)
        recyclerViewUtilKt.initRecyclerView()
        activity.distributorRecycler.useDefaultLoadMore() // 使用默认的加载更多的View。
        activity.distributorRecycler.setLoadMoreListener(mLoadMoreListener) // 加载更多的监听。
        outTreasuryRecordAdapter = OutTreasuryRecordAdapter(activity, R.layout.item_out_treasury_record, outStockBeanList)
        recyclerViewUtilKt.setAdapter(outTreasuryRecordAdapter!!)
        outTreasuryRecordAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(activity, OutTreasuryInfoActivity::class.java)
            intent.putExtra("outStock", outStockBeanList!![position])
            activity.startActivity(intent)
        }
    }


    private val mLoadMoreListener: SwipeMenuRecyclerView.LoadMoreListener = SwipeMenuRecyclerView.LoadMoreListener {
        activity.distributorRecycler.postDelayed({
            getOutTreasury(pageStoreIndex, 10, "", "", "")
        }, 1000)
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            16 -> {
                val outStockList = data as MutableList<OutStock>
                if (pageStoreIndex == 1) {
                    if (outStockList.size >= 0) {
                        outStockBeanList!!.removeAll(outStockBeanList!!)
                        outStockBeanList!!.addAll(outStockList)
                        outTreasuryRecordAdapter?.notifyDataSetChanged()
                        pageStoreIndex++
                        activity.distributorRecycler.loadMoreFinish(false, true)
                    }
                } else {
                    when {
                        outStockList.size == 0 -> activity.distributorRecycler.loadMoreFinish(true, false)
                        outStockList.size > 0 -> {
                            pageStoreIndex++
                            outStockBeanList!!.addAll(outStockList)
                            outTreasuryRecordAdapter?.notifyDataSetChanged()
                            activity.distributorRecycler.loadMoreFinish(false, true)
                        }
                        else -> activity.distributorRecycler.loadMoreError(0, "请求网络失败")
                    }
                }
            }
            34 -> {
                val customerInfoList = data as MutableList<CustomerInfo>
                CustomerInfoPopup(activity, activity.distributorLinear, customerInfoList).setListener(object : CustomerInfoPopup.CustomerInfoListener {
                    override fun handleResult(customerInfo: CustomerInfo) {
                        activity.outTraceStateTV.text = "出库状态"
                        activity.distributorTV.text = customerInfo.CustomerName
                        pageStoreIndex = 1
                        activity.customerID = customerInfo.CustomerID
                        activity.stateID = ""
                        activity.outCode = ""
                        getOutTreasury(1, 999, activity.customerID, activity.stateID, activity.outCode)
                    }
                })
            }
        }
    }


}